(function(){
	
$(function(){

const appSelectRole = {
	template: document.getElementById("template-select-role"),
	data(){
		return {
			Room: null,
			SelectedRole: null,
			Board: null
		}
	},
	created(){
		this.Room = GetUrlParameter("room");
		
		let turnAndPlayerCount = GetValueByMask(this.Room[1]);
		let playerCount = turnAndPlayerCount % 10 + 5;
		
		this.Board = GameBoard[playerCount];
	},
	methods: {
		SelectRole(){
			location.href = `game.html?role=${this.SelectedRole}&room=${this.Room}&turn=1`
		}
	}
};
	
const appGame = {
	template: document.getElementById("template-game"),
	data(){
		return {
			Room: null,
			Role: null,
			Turn: null,
			Leader: null,
			Policy: null,
			Roles: null,
			TestRoles: null,
			GoodRoles: [],
			BadRoles: []
		}
	},
	created(){
		this.Room = GetUrlParameter("room");
		this.Role = parseInt(GetUrlParameter("role"));
		this.Turn = parseInt(GetUrlParameter("turn"));
		
		let mask = GetValueByMask(this.Room[0]);
		
		let turnAndPlayerCount = GetValueByMask(this.Room[1]);
		let playerCount = turnAndPlayerCount % 10 + 5;
		
		this.Board = GameBoard[playerCount];
		
		let date = new Date();
		let seed = this.Room + date.getFullYear() + date.getMonth() + date.getDate();
		
		let random = new Math.seedrandom(seed);
		for(let i=0; i < this.Turn; i++){
			for(let j = 0; j < 7; j++){
				random.quick();
			}
		}
		
		this.Leader = ~~(random.quick() * playerCount) + 1
		
		let policy = {
			NeedPai: !!(mask & 0x01),
			NeedNa: !!(mask & 0x02),
			NeedMo: !!(mask & 0x04),
			NeedAo: !!(mask & 0x08),
			BadShow: !!(mask & 0x10)
		}
		this.Policy = policy;
		
		let roles = ["梅林", "刺客"];
		let good_roles = ["梅林"];
		
		let good = 1;
		if(policy.NeedPai){
			roles.push("派西维尔");
			good_roles.push("派西维尔");
			good++;
		}
		while(good < this.Board.Good){
			roles.push("忠臣");
			good_roles.push("忠臣");
			good++;
		}
		
		let bad_roles = ["刺客"]
		let bad = 1;
		
		if(policy.NeedNa){
			roles.push("莫甘娜");
			bad_roles.push("莫甘娜");
			bad++;
		}
		if(policy.NeedMo){
			roles.push("莫德雷德");
			bad_roles.push("莫德雷德");
			bad++;
		}
		if(policy.NeedAo){
			roles.push("奥伯伦");
			bad_roles.push("奥伯伦");
			bad++;
		}
		let has_zhaoyao = false;
		while(bad < this.Board.Bad){
			roles.push("爪牙");
			bad_roles.push("爪牙");
			has_zhaoyao = true;
			bad++;
		}
		
		this.GoodRoles = good_roles;
		this.BadRoles = bad_roles;
		
		shuffle(random, roles);
		
		let me = roles[this.Role - 1];
		
		let showRoles = [];
		for(let i=1; i<=roles.length; i++){
			let role = roles[i-1];
			
			//自己的身份总是知道
			if(i === this.Role){
				showRoles.push(role);
				continue;
			}

			if(this.IsBad(me) && me !== "奥伯伦"){
				//除奥伯伦以外的坏人队友互相认识
				if(this.IsBad(role) && role !== "奥伯伦"){
					if(policy.BadShow){
						showRoles.push(role);
					}
					//只有2坏人的情况下，显然另一个坏人的身份
					else if(this.Board.Bad === 2 || this.Board.Bad === 3 && policy.NeedAo){
						showRoles.push(role);
					}
					//如果坏人不存在其他神牌，我是刺客，其他必然是爪牙
					else if(!policy.NeedNa && !policy.NeedMo && me === "刺客"){
						showRoles.push(role);
					}
					else{
						showRoles.push("坏人");
					}
					continue;
				}
				
				//在没有奥伯伦的情况下，坏人看到的好人都是好人
				if(this.IsGood(role) && !policy.NeedAo){
					showRoles.push("好人");
					continue;
				}
			}
			
			//梅林知道除莫德雷德以外的坏人牌
			if(me === "梅林"){
				if(this.IsBad(role) && role !== "莫德雷德"){
					showRoles.push("坏人");
					continue;
				}
				//不存在莫德雷德的情况下梅林看到的好人都是好人
				if(this.IsGood(role) && !policy.NeedMo){
					if(policy.NeedPai){
						showRoles.push("好人");
					}
					else{
						showRoles.push("忠臣");
					}
					continue;
				}
			}
			
			//派西维尔知道梅林和莫甘娜
			if(me === "派西维尔"){
				if(role === "梅林"){
					if(policy.NeedNa){
						showRoles.push("梅林 / 莫甘娜");
					}
					else{
						showRoles.push(role);
					}
					continue;
				}
				else if(role === "莫甘娜"){
					showRoles.push("梅林 / 莫甘娜");
					continue;
				}
			}
			
			showRoles.push(null);
		}
		
		this.Roles = showRoles;
		
		let testRoles = [];
		for(let i=1; i<=showRoles.length; i++){
			let role = showRoles[i-1];
			
			let object = {
				Select: 0,
				Values: [role === null || role === "梅林 / 莫甘娜" ? "未知" : role]
			}
			//固定身份的牌不需要抿
			if(this.IsStatic(role)){
				testRoles.push(object);
				continue;
			}
			
			//坏人从坏人牌里面抿
			if(role === "坏人"){
				if(me !== "刺客"){
					object.Values.push("刺客");
				}
				if(policy.NeedMo && me !== "莫德雷德" && me !== "梅林"){
					object.Values.push("莫德雷德");
				}
				if(policy.NeedNa && me !== "莫甘娜"){
					object.Values.push("莫甘娜");
				}
				if(policy.NeedAo && me === "梅林"){
					object.Values.push("奥伯伦");
				}
				if(has_zhaoyao){
					object.Values.push("爪牙");
				}
				testRoles.push(object);
				continue;
			}
			
			//说明啥也不知道
			if(role === "好人"){
				if(this.IsBad(me)){
					object.Values.push("梅林");
					if(policy.NeedPai){
						object.Values.push("派西维尔");
					}
					object.Values.push("忠臣");
				}
				else if(me === "梅林"){
					object.Values.push("派西维尔");
					object.Values.push("忠臣");
				}
				testRoles.push(object);
				continue;
			}
			
			if(role === "梅林 / 莫甘娜"){
				object.Values.push("梅林");
				object.Values.push("莫甘娜");
				testRoles.push(object);
				continue;
			}
			
			//啥也不知道牌
			if(role === null){
				object.Values.push("好人");
				
				//我是坏人的情况下，外置位坏人只有奥伯伦
				if(!this.IsBad(me) && me !== "梅林" || me === "奥伯伦"){
					object.Values.push("坏人");
				}
				
				if(me !== "梅林"){
					object.Values.push("梅林");
				}
				
				if(me !== "派西维尔" && policy.NeedPai){
					object.Values.push("派西维尔");
				}
				
				object.Values.push("忠臣");
				
				if(me !== "奥伯伦" && me !== "梅林" && policy.NeedAo){
					object.Values.push("奥伯伦");
				}
				
				if(!(this.IsBad(me) && me !== "奥伯伦")){
					if(policy.NeedMo && me !== "梅林"){
						object.Values.push("莫甘娜");
					}
					if(policy.NeedMo){
						object.Values.push("莫德雷德");
					}
					if(me !== "梅林"){
						object.Values.push("刺客");
					}
					if(has_zhaoyao && me !== "梅林"){
						object.Values.push("爪牙");
					}
				}
				
				testRoles.push(object);
				continue;
			}
			
			testRoles.push(object);
		}
		this.TestRoles = testRoles;
	},
	computed: {
		MyRole(){
			return this.Roles[this.Role - 1];
		}
	},
	methods: {
		IsGood(role){
			return role === "梅林" || role === "派西维尔" || role === "忠臣" || role === "好人";
		},
		IsBad(role){
			return role === "莫德雷德" || role === "莫甘娜" || role === "刺客" || role === "奥伯伦" || role === "爪牙" || role === "坏人";
		},
		IsStatic(role){
			return role === "梅林" || role === "派西维尔" || role === "忠臣" || role === "莫德雷德" || role === "莫甘娜" || role === "刺客" || role === "奥伯伦"|| role === "爪牙" ;
		},
		Next(){
			location.href = `game.html?room=${this.Room}&role=${this.Role}&turn=${this.Turn+1}`;
		},
		Last(){
			location.href = `game.html?room=${this.Room}&role=${this.Role}&turn=${this.Turn-1}`;
		},
		ChangeTestRole(value, off){
			let i = value.Select + off;
			if(i >= value.Values.length){
				i = 0;
			}
			if(i < 0){
				i = value.Values.length - 1;
			}
			value.Select = i;
		}
	}
}
	
let app = new Vue({
	el: "#app",
	data(){
		return {
			InGame: false
		};
	},
	created(){
		let role = GetUrlParameter("role");
		this.InGame = !!role;
	},
	components: {
		"select-role": appSelectRole,
		"game": appGame
	}
});

});
	
function shuffle(random, array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  while (0 !== currentIndex) {
    randomIndex = Math.floor(random.quick() * currentIndex);
    currentIndex -= 1;

    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
	
})();